#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   xml_analysis.py    
@Contact :   291622538@qq.com

@Modify Time      @Author    @Version    @Description
------------      -------    --------    -----------
2021/3/31 20:21   fan        1.0         None
"""
import csv
import os
import requests
from xml.dom import minidom
import json


def save_file(path, item):
    # 先将字典对象转化为可写入文本的字符串
    item = json.dumps(item, ensure_ascii=False)

    try:
        if not os.path.exists(path):
            with open(path, "w", encoding='utf-8') as f:
                f.write(item + "\n")
                print("^_^ write success")
        else:
            with open(path, "a", encoding='utf-8') as f:
                f.write(item + "\n")
                print("^_^ write success")
    except Exception as e:
        print("write error==>", e)


def get_domain_info(url):
    domain_info = {}
    # 对需要爬取的网页发送请求
    response = requests.get('http://data.alexa.com/data?cli=10&dat=snbamz&url=%s' % url)
    response.encoding = 'utf-8'
    # 打印读取的内容
    data = str(response.text)
    # 将xml数据解析成dom文件
    root = minidom.parseString(data)
    # 根据标签名获取标签
    country_code = root.getElementsByTagName('COUNTRY')[0].getAttribute("CODE")
    country_name = root.getElementsByTagName('COUNTRY')[0].getAttribute("NAME")
    country_rank = root.getElementsByTagName('COUNTRY')[0].getAttribute("RANK")
    owner_name = root.getElementsByTagName('OWNER')[0].getAttribute("NAME") if root.getElementsByTagName(
        'OWNER') else ""
    popularity_rank = root.getElementsByTagName('POPULARITY')[0].getAttribute("TEXT")
    reach_rank = root.getElementsByTagName('REACH')[0].getAttribute("RANK")
    # print(country_code)
    # print(country_name)
    # print(country_rank)
    # print(owner_name)
    # print(popularity_rank)
    # print(reach_rank)
    rls_list = []
    rls_prefix = root.getElementsByTagName('RLS')[0].getAttribute("PREFIX")
    for rls in root.getElementsByTagName('RL'):
        href = rls_prefix + rls.getAttribute("HREF")
        # print(href)
        title = rls.getAttribute("TITLE")
        # print(title)
        rls_list.extend([href, title])
    domain_info["country_code"] = country_code
    domain_info["country_name"] = country_name
    domain_info["country_rank"] = country_rank
    domain_info["owner_name"] = owner_name
    domain_info["popularity_rank"] = popularity_rank
    domain_info["reach_rank"] = reach_rank
    domain_info["rls_list"] = rls_list

    # print(domain_info)
    save_file('../data/xml_domain_info.json', domain_info)


if __name__ == '__main__':

    with open('../root_data/top-1m.csv', 'r', encoding='utf-8') as f:
        for num, line in enumerate(csv.reader(f)):

            if num == 10:
                break
            domain_name = line[1]
            get_domain_info(domain_name)
